<?php
class Location extends Controller {
    
    function Location()
    {
        parent::Controller();
        $this->load->helper(array('form','tiap_helper'));
        $this->load->library('form_validation'); 
        $this->load->model('Model_location');    
    }
    
    function index()
    {        
        $test = $this->Model_location->get_location(1);
        $test['error'] = '';        
        $test['classonlocation'] = "1";
        $test['upload_data'] = "";
        
        if(empty($test)){
            // intial value
            $test['user_tiab_id'] = "";        
            $test['latitude'] = "";
            $test['longitude'] = "";
            $test['address'] = "";        
            $test['city'] = "";
            $test['radius'] = "";
            $test['radius_type'] = "";
        }
        
        //echo "<pre>";print_r($test);echo "</pre>";
        $this->load->view(TEMPLATE_NAME.'/front/profile/location' , $test);                 
    }
    
    function submit()
    {
        $this->form_validation->set_rules('address', 'Address', 'required');
        $this->form_validation->set_rules('city', 'City', 'required');
        
        if ($this->form_validation->run() == FALSE)
        {
            $test = $this->Model_location->get_location(1);
            if(empty($test)){ 
                // intial value
                $test['user_tiab_id'] = "";        
                $test['latitude'] = "";
                $test['longitude'] = "";
                $test['address'] = "";        
                $test['city'] = "";
                $test['radius'] = "";
                $test['radius_type'] = "";
            } 
                        
            $test['error'] = '1';            
            $test['classonlocation'] = "1";
            $test['upload_data'] = "";            
                       
            $this->load->view(TEMPLATE_NAME.'/front/profile/location', $test);
        }
        else
        {                    
            $data = $this->Model_location->get_location($this->input->post('user_tiab_id'));
            
            if($data){
                
                $form = array(
                'user_tiab_id'  => $this->input->post('user_tiab_id'),
                'latitude'      => $this->input->post('latitude'),
                'longitude'     => $this->input->post('longitude'),
                'address'       => $this->input->post('address'),
                'city'          => $this->input->post('city'),
                'radius'        => $this->input->post('radius'),
                'radius_type'   => $this->input->post('type'),
                );
                $this->Model_location->update_location($data["id"],$form);
            }else{            
                
                $user_tiab_id   = $this->input->post('user_tiab_id');
                $latitude       = $this->input->post('latitude');
                $longitude      = $this->input->post('longitude');
                $address        = $this->input->post('address');
                $city           = $this->input->post('city');
                $radius         = $this->input->post('radius');
                $radius_type    = $this->input->post('type');
                
                $this->Model_location->insert_location($user_tiab_id, $latitude, $longitude, $address, $city, $radius, $radius_type);   
            }        
            
            $test = $this->Model_location->get_location(1);
            $test['error'] = '';            
            $test['classonlocation'] = "1";
            $test['upload_data'] = "1";
                                
            $this->load->view(TEMPLATE_NAME.'/front/profile/location', $test);
        }
    }
}
?>
